package com.outsider.news.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.jfinal.weixin.sdk.msg.out.News;
import com.outsider.util.Const;

public interface BaseNews {
    public News getNews();
    public boolean equals(Object o);
    public List<BaseNews> findAll();
    public String getTitle();
    public boolean isMatchKeywords(BaseNews baseNews,String keyword);
    
    public default List<BaseNews> findKeyWordNews(String[] keywords, Set<BaseNews> set) {
    	Set<BaseNews> allSet=new HashSet<>(findAll());
    	allSet.removeAll(set);
		List<BaseNews> result=new ArrayList<>();
		Iterator<BaseNews> iter=allSet.iterator();
		if (keywords!=null){
		while(iter.hasNext()){
			BaseNews baseNews=iter.next();
			if (result.size()==Const.MAX_NEWS_SIZE) break;
			for (int i=0;i<keywords.length;i++)
				if (isMatchKeywords(baseNews,keywords[i])){
					set.add(baseNews);
					result.add(baseNews);
					iter.remove();
					break;
				}
		}
		}
		int remain=Const.MAX_NEWS_SIZE-result.size();
		if (remain==0) return result;
		else{
			iter=allSet.iterator();
			while (iter.hasNext()){
				BaseNews baseNews=iter.next();
				set.add(baseNews);
				result.add(baseNews);
				remain--;
				if (remain==0) return result;
			}
			return result;
		}

	}
}

